<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 订单管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>商城订单</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="4">
                        <el-button
                                type="danger"
                                icon="el-icon-delete"
                                class="handle-del mr10"
                                @click="delAllSelection"
                        >批量删除</el-button>
                    </el-col>
                    <el-col :span="3">
                        <el-date-picker
                                v-model="query.start_time"
                                type="datetime"
                                placeholder="开始时间">
                        </el-date-picker>
                    </el-col>
                    <el-col :span="3">
                        <el-date-picker
                                v-model="query.end_time"
                                type="datetime"
                                placeholder="结束时间">
                        </el-date-picker>
                    </el-col>
                    <el-col :span="3"  style="margin-left: 3%">
                        <el-select v-model="query.sort" placeholder="选择排序方式">
                            <el-option key="1" label="最新" value="1"></el-option>
                            <el-option key="2" label="正常" value="2"></el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="3">
                        <el-select
                                v-model="query.status"
                                placeholder="状态"
                                class="handle-select mr10"
                        >
                            <el-option label="全部状态" value=""></el-option>
                            <el-option key="0" label="待支付" value="0"></el-option>
                            <el-option key="1" label="已支付" value="1"></el-option>
                            <el-option key="2" label="已退款" value="2"></el-option>
                            <el-option key="3" label="售后维权" value="3"></el-option>
                            <el-option key="4" label="已完成" value="4"></el-option>
                            <el-option key="5" label="已取消" value="5"></el-option>
                            <el-option key="6" label="服务中" value="6"></el-option>
                        </el-select>
                    </el-col>

                    <el-col :span="4" style="margin-left: 6%;display: flex;">
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                        <el-button type="primary" @click="query.status ='';query.start_time='';query.end_time='';query.sort == 1">重置</el-button>
                        <el-button type="primary" icon="el-icon-download" @click="exportSource">导出数据</el-button>
                    </el-col>
                </el-row>
            </div>
            <el-table
                    id="repottable"
                    :data="tableData"
                    border
                    v-loading="loading"
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" sortable width="55" align="center"></el-table-column>
                <el-table-column prop="order_sn" label="订单编号" width="150" align="center"></el-table-column>
                <el-table-column label="用户" prop="mobile" align="center"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template
                            slot-scope="scope"
                    >{{ scope.row.status_name}}</template>
                </el-table-column>
                <el-table-column label="支付方式" align="center">
                    <template
                            slot-scope="scope"
                    >{{ scope.row.pay_type}}</template>
                </el-table-column>
                <el-table-column label="商品" align="center">
                    <template slot-scope="scope">
                        <el-image
                                class="table-td-thumb"
                                :src="scope.row.goods_info.banner"
                        ></el-image>
                        <span>{{scope.row.goods_info.name}}</span>
                    </template>
                </el-table-column>
                <!--<el-table-column prop="price" label="金额"></el-table-column>-->
                <el-table-column prop="total_price" label="订单总价"></el-table-column>
                <el-table-column prop="pay_price" label="实际付款"></el-table-column>
                <el-table-column prop="red_price" label="红包优惠"></el-table-column>
                <el-table-column prop="created_at" label="创建时间"></el-table-column>
                <el-table-column prop="pay_at" label="支付时间"></el-table-column>
                <el-table-column prop="finish_at" label="完成时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                         <!--<el-button-->
                            <!--type="text"-->
                            <!--icon="el-icon-edit"-->
                            <!--@click="handleEdit(scope.$index, scope.row,6)"-->
                            <!--v-if="scope.row.status==1"-->
                        <!--&gt;开始接单</el-button>-->
                        <!--<el-button-->
                                <!--type="text"-->
                                <!--icon="el-icon-edit"-->
                                <!--@click="handleEdit(scope.$index, scope.row,4)"-->
                                <!--v-if="scope.row.status==6"-->
                        <!--&gt;完成代练</el-button>-->
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="InfoView(scope.$index, scope.row)"
                        >查看明细</el-button>
                        <!-- <el-button
                            type="text"
                            icon="el-icon-edit"
                            style="color:#E6A23C"
                            @click="handleEdit(scope.$index, scope.row,2)"
                            v-if="scope.row.status==0"
                        >拒绝审核</el-button> -->
                        <!-- <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next,sizes"
                        :page-sizes="[0,10, 20, 50,100 ,500,1000]"
                        @size-change="handleSizeChange"
                        :current-page="query.page"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <el-dialog title="订单详细信息" v-dialogDrag :visible.sync="InfoVisible" width="65%">
            <el-form ref="edit" :model="order_info" label-width="90px">
                <el-row>
                    <el-col><span class="gl_t">创建时间：</span>{{order_info.created_at}}</el-col>
                    <el-col><span class="gl_t">开始时间：</span>{{order_info.created_at}}</el-col>
                    <el-col><span class="gl_t">结束时间：</span>{{order_info.end_at}}</el-col>
                    <el-col v-if="order_info.belong == 1"><span class="gl_t" >租赁总时长：</span>{{order_info.num}}小时</el-col>
                    <el-col><span class="gl_t">支付时间：</span>{{order_info.pay_at}}</el-col>
                    <el-col><span class="gl_t">完成时间：</span>{{order_info.finish_at}}</el-col>
                    <el-col><span class="gl_t">支付方式：</span>{{order_info.pay_type}}</el-col>
                    <el-col><span class="gl_t">关联游戏：</span>{{order_info.game_name}}</el-col>
                    <el-col v-if="order_info.belong == 1">
                        <span class="gl_t">关联商品：</span>{{order_info.goods_info.name}}，单价：￥{{order_info.price}}
                    </el-col>
                    <el-col><span class="gl_t">订单总价：</span>￥{{order_info.total_price}}</el-col>
                    <el-col><span class="gl_t">红包优惠：</span>￥{{order_info.red_price}}</el-col>
                    <el-col><span class="gl_t">实际付款：</span>￥{{order_info.pay_price}}</el-col>
                    <el-col><span class="gl_t">用户信息：</span>手机号：{{order_info.user_info.mobile}},昵称：{{order_info.user_info.nickname}}</el-col>
                    <el-row v-if="order_info.belong == 3">
                        <el-col v-if="order_info.game_id == 1"><span class="gl_t" >商城订单信息：</span>
                          {{order_info.ext_name}}
                        </el-col>
                    </el-row>

                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="InfoVisible = false">关 闭</el-button>
            </span>
        </el-dialog>
        <!-- 编辑发货 -->
        <el-dialog title="编辑发货信息" v-dialogDrag :visible.sync="deliverVisible" width="580px">
            <el-form ref="edit" :model="deliver" label-width="90px">
                <el-form-item label="发货说明" class="form-item">
                    <el-input
                            v-model="deliver.deliver_desc"
                            maxlength="30"
                            type="text"
                            minlength="2"
                            placeholder="请输入发货说明,尽量字数控制20字以内"
                    ></el-input>
                </el-form-item>
                <el-form-item label="发货单号" class="form-item">
                    <el-input
                            v-model="deliver.deliver_sn"
                            maxlength="30"
                            type="text"
                            minlength="2"
                            placeholder="请输入发货单号,尽量不要填写无关内容"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="deliverVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveDeliver">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

    export default {
        name: 'dl_game_order',
        data () {
            return {
                query: {
                    page: 1,
                    pageSize: 10,
                    status: '',
                    sort: '1',
                    belong:3
                },
                deliverVisible: false,
                InfoVisible: false,
                deliver: {}, order_info: {'goods_info':{'banner':'','name':''},'user_info':{'mobile':'','nickname':''}},
                loading: false,
                tableData: [],
                multipleSelection: [],
                pageTotal: 0,
                delList: [],
                idx: -1,
                id: -1,
                //获取列表请求
                getList: function () {
                    let that = this;
                    this.api.get('getGameOrder', this.query, res => {
                        for (let i in res.data.list) {
                            res.data.list[i].status = res.data.list[i].status + '';
                        }
                        that.tableData = res.data.list;
                        that.query.page = res.data.page;
                        that.query.pageSize = res.data.pageSize;
                        that.pageTotal = res.data.pageTotal;
                    }, undefined, undefined, this);
                },
                //删除请求
                delLists: function (ids, msg) {
                    let that = this;
                    if (ids.length < 1) {
                        this.$message.error('请选择删除的数据');
                        return false;
                    }
                    // 二次确认删除
                    this.$confirm('确定要删除吗？', '提示', {
                        type: 'warning'
                    }).then(() => {
                        that.api.delete('delReport', { ids: ids }, res => {
                            that.query.page = 1;
                            that.getList();
                            that.multipleSelection = [];
                            that.$message.success(res.msg);
                        }, undefined, undefined, this);
                    }).catch(() => { });
                }
            };
        },
        created () {
            this.getList();

        },
        methods: {
            // 触发搜索按钮
            handleSearch () {
                this.$set(this.query, 'page', 1);
                this.getList();
            },
            exportSource(){
                this.api.exportExcel(document.querySelector('#repottable'),'兑换订单数据表')
            },
            handleSizeChange(val){
                console.log(val)
                if(val == 0){
                    this.query.pageSize = 100000000;
                }else{
                    this.query.pageSize = val;
                }
                this.getList();
            },
            handleEdit (index, row, status) {
                this.$confirm('确定要' + (status == 6 ? '开始接单' : '完成代练') + '吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.api.put('dlOrderStatus', { id: row.id, status: status }, res => {
                        this.tableData[index].status = status;
                        this.$message.success(res.msg);
                        this.getList();
                    });
                }).catch(() => { });
            },
            InfoView (index, row) {
                this.order_info = row
                console.log(this.order_info)
                this.InfoVisible = true;
            },

            /**
             * 复制文本
             */
            copy (text) {
                if (!text || text == '' || text == ' ') {
                    this.$message.error('没有可复制的信息');
                    return false;
                }
                this.$copyText(text).then(() => {
                    this.$message.success('复制成功');
                }, () => {
                    this.$message.error('复制失败，当前浏览器不支持复制');
                });
            },

            /**
             * 保存发货信息
             */
            saveDeliver () {
                this.api.put('report-deliver', this.deliver, res => {
                    this.deliverVisible = false;
                    this.$message.success('保存成功');
                    this.getList();
                }, undefined, undefined, this);
            },

            showText (title, text) {
                this.$alert(text, title, {
                    confirmButtonText: '确定',
                    callback: action => {

                    }
                });
            },
            // 多选操作
            handleSelectionChange (val) {
                this.multipleSelection = val;
            },

            // 删除操作
            handleDelete (index, row) {
                this.delLists([row.id], '删除成功');
            },
            //批量删除
            delAllSelection () {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                let ids = [];
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                    ids.push(this.multipleSelection[i].id);
                }
                this.delLists(ids, `删除了${str}`);
            },

            // 分页导航
            handlePageChange (val) {
                this.$set(this.query, 'page', val);
                this.getList();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 150px;
    }
    .show-text {
        cursor: pointer;
        color: #75c0ed;
    }
    .show-text:hover {
        color: #2e91cd;
    }
    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
    .gl_t{
        font-weight: bold;
    }
</style>
